package com.shilin.watchsystem.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shilin.watchsystem.entity.Product;
import com.shilin.watchsystem.entity.Worder;
import com.shilin.watchsystem.form.AddOrderForm;
import com.shilin.watchsystem.form.EditOrderForm;
import com.shilin.watchsystem.form.EditProductForm;
import com.shilin.watchsystem.service.IOrderService;
import com.shilin.watchsystem.util.ResultUtil;
import com.shilin.watchsystem.vo.OrderPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-08-21
 */
@RestController
@RequestMapping("/watchsystem/order")
public class OrderController {

    @Autowired
    IOrderService orderService;

    @PostMapping("/insert")
    public Object insert(AddOrderForm form, HttpServletRequest request){
        orderService.insert(form,request);
        return ResultUtil.success();
    }

    @PostMapping("/update")
    public Object update(EditOrderForm form, HttpServletRequest request){
        orderService.update(form);
        return ResultUtil.success();
    }

    @PostMapping("/select")
    public Object select(Long id, HttpServletRequest request){
        OrderPageVo vo = orderService.select(id);
        return ResultUtil.success(vo);
    }

    @PostMapping("/delete")
    public Object delete(Long id, HttpServletRequest request){
        orderService.removeById(id);
        return ResultUtil.success();
    }

    @PostMapping("/page")
    public Object page(int page,int limit){
        Page<Worder> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(limit);
        IPage<OrderPageVo> vos = orderService.page(page1);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",vos.getRecords().size());
        map.put("msg","成功");
        map.put("data",vos.getRecords());
        return map;
    }

}
